package net.hust.atom.geotag;

import java.util.List;

import android.content.Context;
import android.location.Address;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MnMapSearchAdapter extends BaseAdapter{

	private Context mContext;
	private List<Address> mAddresses;
	
	private int mLayout;
	
	public MnMapSearchAdapter(Context context, List<Address> addresses){
		this.mAddresses = addresses;
		this.mContext = context;
		mLayout = R.layout.geo_search_list_item;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		if(convertView == null){
			v = newView(mContext, parent);
		}else{
			v = convertView;
		}
		
		bindView(v, mContext, mAddresses.get(position));
		
		return v;
	}
	
	public View newView(Context context, ViewGroup parent){
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(mLayout, parent, false);
		
		Holder holder = new Holder();
		holder.lLayoutParent = (LinearLayout)view.findViewById(R.id.geo_search_list_layout);
		holder.geoSearchImg = (ImageView)view.findViewById(R.id.geo_search_list_item_icon);
		holder.geoSearchText = (TextView)view.findViewById(R.id.geo_search_list_item_text);
		view.setTag(holder);
		return view;
	}
	
	private static class Holder{
		LinearLayout lLayoutParent;
		ImageView geoSearchImg;
		TextView geoSearchText;
	}
	
	public void bindView(View view, Context context, Address address){
		Holder holder = (Holder)view.getTag();
		StringBuilder sb = new StringBuilder();
		if(address.getCountryCode() != null){
			// 국가 코드 ex) KR
		}
		if(address.getCountryName() != null){
			// 국가 이름 ex) 대한민국
			sb.append(address.getCountryName()).append(" ");
		}
		if(address.getLocality() != null){
			// 시 ex) 서울특별시
			sb.append(address.getLocality()).append(" ");
		}
		if(address.getThoroughfare() != null){
			// 동 ex) 역삼동
			sb.append(address.getThoroughfare()).append(" ");
		}
		if(address.getPostalCode() != null){
			sb.append(address.getPostalCode()).append(" ");
			// 우편번호 ex) 135-081
		}
		if(address.getAdminArea() != null){
			Log.i("ATOM", "address.getAdminArea() : " + address.getAdminArea());
		}
		if(address.getFeatureName() != null){
			// 번지 or 동 ex) 801-47 or 역삼1동
		}
		if(address.getPhone() != null){
			Log.i("ATOM", "address.getPhone() : " + address.getPhone());
		}
		if(address.getPremises() != null){
			Log.i("ATOM", "address.getPremises() : " + address.getPremises());
		}
		if(address.getSubAdminArea() != null){
			Log.i("ATOM", "address.getSubAdminArea() : " + address.getSubAdminArea());
		}
		if(address.getSubLocality() != null){
			Log.i("ATOM", "address.getSubLocality() : " + address.getSubLocality());
		}
		if(address.getSubThoroughfare() != null){
			Log.i("ATOM", "address.getSubThoroughfare() : " + address.getSubThoroughfare());
		}
		holder.geoSearchText.setText(sb.toString());
	}

	@Override
	public int getCount() {
		return mAddresses.size();
	}

	@Override
	public Object getItem(int position) {
		return mAddresses.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}